package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.AndroidUser;
import com.example.demo.mapper.AndroidUserMapper;
import com.example.demo.util.Msg;
import com.example.demo.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/AndroidUser")
public class AndroidUserController {

    @Autowired
    private AndroidUserMapper androidUserMapper;

    /**
     * 登录接口
     * @param androidUser
     * @return
     */
    @PostMapping(value = "/login", produces = MediaType.APPLICATION_JSON_VALUE)
    public Msg login(@RequestBody AndroidUser androidUser){
        try {
            if(androidUser.getUserName() == null || androidUser.getPwd() == null)
                return ResultUtil.error(500,"用户名或密码不能为空");
            QueryWrapper<AndroidUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_name", androidUser.getUserName());
            queryWrapper.eq("pwd", androidUser.getPwd());
            AndroidUser loginResult=androidUserMapper.selectOne(queryWrapper);
//            System.out.println(loginResult);
            if(loginResult==null){
                return ResultUtil.error(500,"用户名或密码错误");
            } else {
                System.out.println(loginResult.getId());
                return ResultUtil.success(String.valueOf(loginResult.getId()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(400,"出现异常");
        }
    }

    /**
     * 注册
     * @param androidUser
     * @return
     */
    @PostMapping(value = "/register", produces = MediaType.APPLICATION_JSON_VALUE)
//    @Transactional
    public Msg register(@RequestBody AndroidUser androidUser){
        if(androidUser.getUserName() == null || androidUser.getPwd() == null)
            return ResultUtil.error(500,"用户名或密码不能为空");
        AndroidUser androidUser1 = new AndroidUser();
        try{
            QueryWrapper<AndroidUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_name",androidUser.getUserName());
            AndroidUser registerResult = androidUserMapper.selectOne(queryWrapper);
            if (registerResult != null)
                return ResultUtil.error(500,"用户名已注册");
            androidUser1.setUserName(androidUser.getUserName());
            androidUser1.setPwd(androidUser.getPwd());
//            user1.setPhone(user.getPhone());
            int res = androidUserMapper.insert(androidUser1);
            System.out.println(res);
        } catch (Exception e){
            return ResultUtil.error(400,"出现异常");
        }
        return ResultUtil.success();
    }

    /**
     * 获取单个用户信息
     * @param userName
     * @return
     */
    @GetMapping(value = "/getUser")
    public Msg getUser(String userName){
        QueryWrapper<AndroidUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",userName);
        AndroidUser res = androidUserMapper.selectOne(queryWrapper);
        if (res == null)
            return ResultUtil.error(500,"获取数据失败");
        else
            return ResultUtil.success(res);
    }

    /**
     * 修改用户密码
     * @param androidUser
     * @return
     */
    @PostMapping(value = "/updatePwd", produces = MediaType.APPLICATION_JSON_VALUE)
//    @Transactional
    public Msg updatePwd(@RequestBody AndroidUser androidUser){
//        Integer id;
        QueryWrapper<AndroidUser> queryWrapper1 = new QueryWrapper<>();
        AndroidUser androidUser1 = new AndroidUser();
        if(androidUser.getOldpwd() == null || androidUser.getNewpwd() == null)
            return ResultUtil.error(500,"密码不能为空");
        else if(androidUser.getUserName() == null)
            return ResultUtil.error(500,"用户名为空");
        try{
            QueryWrapper<AndroidUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_name",androidUser.getUserName()).eq("pwd",androidUser.getOldpwd());
            AndroidUser res = androidUserMapper.selectOne(queryWrapper);
            if (res == null)
                return ResultUtil.error(500,"旧密码错误");
            else{
//                id = res.getId();
                queryWrapper1.eq("user_name",androidUser.getUserName());
                androidUser1.setPwd(androidUser.getNewpwd());
                int res2 = androidUserMapper.update(androidUser1,queryWrapper);
                if(res2 == 1)
                    return ResultUtil.success(res2);
                else
                    return ResultUtil.error(500,"修改密码失败");
            }
        }catch (Exception e){
            return ResultUtil.error(400,"出现异常");
        }

    }

    /**
     * 设置密保
     * @param androidUser
     * @return
     */
    @PostMapping(value = "/SecuritysetAdd", produces = MediaType.APPLICATION_JSON_VALUE)
    public Msg SecuritysetAdd(@RequestBody AndroidUser androidUser){
        try {
            if (androidUser.getUserName() == null || androidUser.getAnswer() == null)
                return ResultUtil.error(500, "密保不能为空");
            QueryWrapper<AndroidUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_name", androidUser.getUserName());
            AndroidUser androidUser1 = new AndroidUser();
            androidUser1.setAnswer(androidUser.getAnswer());
            int res = androidUserMapper.update(androidUser1, queryWrapper);
            if (res == 1)
                return ResultUtil.success(res);
            else
                return ResultUtil.error(500, "密保设置失败");
        } catch (Exception e) {
            return ResultUtil.error(400, "出现异常");
        }
    }

    /**
     * 重置密码
     *
     * @param androidUser
     * @return
     */
    @PostMapping(value = "/resetPwd", produces = MediaType.APPLICATION_JSON_VALUE)
    public Msg resetPwd(@RequestBody AndroidUser androidUser){
        try {
//            if (androidUser.getUserName() == null || androidUser.getAnswer() == null)
//                return ResultUtil.error(500, "提交参数错误");
            QueryWrapper<AndroidUser> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("user_name",androidUser.getUserName()).eq("answer",androidUser.getAnswer());
            AndroidUser res1 = androidUserMapper.selectOne(queryWrapper1);
            if (res1 == null)
                return ResultUtil.error(500,"密保错误");
            if (androidUser.getPwd() == null)
                androidUser.setPwd("123456");
            QueryWrapper<AndroidUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_name", androidUser.getUserName());
            int res = androidUserMapper.update(androidUser, queryWrapper);
            if (res == 1)
                return ResultUtil.success(res);
            else
                return ResultUtil.error(500, "密码重置失败");
        }catch (Exception e){
            return ResultUtil.error(400,"出现异常");
        }
    }

    /**
     * 修改个人信息
     * @param androidUser
     * @return
     */
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public Msg update(@RequestBody AndroidUser androidUser){
        try{
//            if (androidUser.getUserName() == null)
//                return ResultUtil.error(500,"用户名不能为空");
//            QueryWrapper<AndroidUser> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("id",androidUser.getId());
            AndroidUser androidUser1 = new AndroidUser();
            int res = androidUserMapper.updateById(androidUser);
            if (res == 1) {
//                return ResultUtil.success(res);
                androidUser1 = androidUserMapper.selectById(androidUser.getId());
                System.out.println(androidUser1);
                return ResultUtil.success(androidUser1);
            }
            else
                return ResultUtil.error(500,"个人信息修改失败");
        }catch (Exception e){
            return ResultUtil.error(400,"出现异常");
        }

    }


}
